<?php
function page_home(){
  $output = page_home_slideshow();
  $output .= page_home_hot_product();
  $output .= page_home_hot_shop();
  $output .= page_home_hot_brand();
  $output .= page_home_hot_motivate();
  return $output;
}

function page_home_slideshow(){
  return '';
}

function page_home_hot_product(){
  $types = f_node_get_product_type();
  $select = db_select('node')
            ->fields('node', array('nid'))
            ->condition('type', $types, 'in')
            ->condition('status', 1)
            ->range(0, 5)
            ->execute()
            ->fetchCol();
  if($select){
    $items = array();
    $nodes = node_load_multiple($select);
    foreach ($nodes as $value) {
      $path = 'public://default/product.jpg';
      if(isset($value->field_image[$value->language][0]['uri'])){
        $path = $value->field_image[$value->language][0]['uri'];
      }
      $image = theme('image_style', array('style_name' => 'thumb_144x215', 'path' => $path));
      $items[$value->nid]['photo'] = l($image, 'node/'.$value->nid, array('html' => true));
      $items[$value->nid]['title'] = l($value->title, 'node/'.$value->nid, 
              array('attributes' => array('class' => array('title'))));
      $user = user_load($value->uid);
      $items[$value->nid]['name'] = theme('username', array('account' => $user, 'class' => 'postby'));
      $items[$value->nid]['price'] = isset($value->field_price[$value->language])?
                  $value->field_price[$value->language][0]['value']:0;
      $items[$value->nid]['price_unit'] = isset($value->field_price_unit[$value->language])?
                  $value->field_price_unit[$value->language][0]['value']:"VND";
    }
    return theme('home_product_list', array('items' => $items));
  }
  return '';
}

function page_home_hot_shop(){
  $types = f_node_get_product_type();
  $select = db_select('node')
            ->fields('node', array('nid'))
            ->condition('type', 'shop')
            ->condition('status', 1)
            ->range(0, 5)
            ->execute()
            ->fetchCol();
  if($select){
    $items = array();
    $nodes = node_load_multiple($select);
    foreach ($nodes as $value) {
      $path = 'public://default/shop.jpg';
      if(isset($value->field_logo[$value->language][0]['uri'])){
        $path = $value->field_logo[$value->language][0]['uri'];
      }
      $image = theme('image_style', array('style_name' => 'thumb_145x125', 'path' => $path));
      $items[$value->nid]['photo'] = l($image, 'node/'.$value->nid, array('html' => true));
      $items[$value->nid]['title'] = l($value->title, 'node/'.$value->nid, 
              array('attributes' => array('class' => array('title'))));
      $user = user_load($value->uid);
      $items[$value->nid]['name'] = theme('username', array('account' => $user));
    }
    return theme('home_shop_list', array('items' => $items));
  }
  return '';
}

function page_home_hot_brand(){
  $types = f_node_get_product_type();
  $select = db_select('node')
            ->fields('node', array('nid'))
            ->condition('type', 'brand')
            ->condition('status', 1)
            ->range(0, 15)
            ->execute()
            ->fetchCol();
  if($select){
    $items = array();
    $nodes = node_load_multiple($select);
    foreach ($nodes as $value) {
      $path = 'public://default/brand.jpg';
      if(isset($value->field_logo[$value->language][0]['uri'])){
        $path = $value->field_logo[$value->language][0]['uri'];
      }
      $image = theme('image_style', array('style_name' => 'thumb_110x60', 'path' => $path));
      $items[] = l($image, 'node/'.$value->nid, array('html' => true  ));
    }
    return theme('home_brand_list', array('items' => $items));
  }
  return '';
}

function page_home_hot_motivate(){
  $path = path_to_theme().'/images/congdong.jpg';
  return theme('image', array('path' => $path));
}